import tkinter as tk
from asr_bt import BTSystem
from multiprocessing import Process,current_process,Queue
import tkinter.messagebox as messagebox
from utils.datasets import validateDatasetDir,get_dataset_component_info

class Gui(tk.Tk):
    def __init__(self,*args,**kw):
        super(Gui,self).__init__()
        self.wm_attributes('-topmost',1)
        self.btsystem=BTSystem()
        self.q=Queue()
        bt_proc=Process(target=self.btsystem.main,args=(self.q,))
        bt_proc.daemon=True
        bt_proc.start()
        self.statusLabel=tk.Label(self,text='')
        self.statusLabel.pack()
        # print(self.btsystem.getStatus())
        self.showStatus()
        self.quitButton=tk.Button(self,text='退出',command=self.quit)
        self.quitButton.pack()
        self.addDatasetButton=tk.Button(self,text='添加数据集',command=self.addDataset)
        self.addDatasetButton.pack()
        self.addModelButton=tk.Button(self,text='添加模型',command=self.addModel)
        self.addModelButton.pack()
        self.protocol('WM_DELETE_WINDOW',self.not_close)
        self.mainloop()

    def showStatus(self):
        # print(self.btsystem.getStatus())
        if not self.q.empty():
            while not self.q.empty():
                status=self.q.get()
            self.statusLabel.configure(text=status)
        self.after(1000,self.showStatus)

    def not_close(self):
        messagebox.showwarning('警告','请通过退出按钮关闭窗口')

    def quit(self):
        if self.btsystem.status=='idle':
            self.destroy()
        else:
            quitStatus=messagebox.askquestion('询问','你确定退出？')
            if quitStatus=='yes':
                # TODO 退出前的准备工作
                self.destroy()

    def addDataset(self):
        self.datasetDir=''
        self.datasetName=''
        datasetWindow=tk.Toplevel(self)
        datasetDirLabel=tk.Label(datasetWindow,text='数据集目录')
        datasetDirLabel.pack()
        addDatasetDirButton=tk.Button(datasetWindow,text='添加数据集目录',command=self.addDatasetDir)
        addDatasetDirButton.pack()
        self.datasetDirEntry=tk.Entry(datasetWindow)
        self.datasetDirEntry.pack()
        datasetNameLabel=tk.Label(datasetWindow,text='数据集唯一名称')
        datasetNameLabel.pack()
        addDatasetNameButton=tk.Button(datasetWindow,text='添加数据集名称',command=self.addDatasetName)
        addDatasetNameButton.pack()
        self.datasetNameEntry=tk.Entry(datasetWindow,state='readonly')
        self.datasetNameEntry.pack()
        datasetUrlLabel=tk.Label(datasetWindow,text='数据集URL，选填')
        datasetUrlLabel.pack()
        self.datasetUrlEntry=tk.Entry(datasetWindow,state='readonly')
        self.datasetUrlEntry.pack()
        datasetDescLabel=tk.Label(datasetWindow,text='数据集描述，选填')
        datasetDescLabel.pack()
        self.datasetDescEntry=tk.Entry(datasetWindow)
        self.datasetDescEntry.pack()
        def datasetWindowExit():
            datasetWindow.destroy()
            datasetWindow.update()
        def datasetWindowSubmit():
            self.submitDatasetInfomation()
            datasetWindowExit()
        datasetWindowCloseButton=tk.Button(datasetWindow,text='关闭',command=datasetWindowExit)
        datasetWindowCloseButton.pack()
        datasetWindowSubmitButton=tk.Button(datasetWindow,text='提交',command=datasetWindowSubmit)
        datasetWindowSubmitButton.pack()

    def addDatasetDir(self):
        self.datasetDir=tk.filedialog.askdirectory()
        if self.datasetDir!='':
            validateResult=validateDatasetDir(datasetDir)
            if validateResult is not None:
                messagebox.showerror('校验数据集目录出现问题',validateResult)
                self.datasetDir=''
            else:
                self.datasetDirEntry.delete(0,'end')
                self.datasetDirEntry.insert(0,self.datasetDir)

    def addDatasetName(self):
        ok=False
        while not ok:
            self.datasetName=tk.simpledialog.askstring('询问','数据集唯一名称')
            if self.datasetName!='' and not os.path.exists(os.path.join('jsons/datasets',self.datasetName+'.json')):
                ok=True
                self.datasetNameEntry.delete(0,'end')
                self.datasetNameEntry.insert(0,self.datasetName)
            else:
                messagebox.showerror('错误','数据集名称为空或已存在')
                self.datasetName=''
    
    def submitDatasetInfomation(self):
        datasetConfDict={'name':self.datasetName,'url':self.datasetUrlEntry.get(),'description':self.datasetDescEntry.get(),'path':self.datasetDir,'components':{}}
        get_dataset_component_info(datasetConfDict)

    def addModel(self):
        pass

if __name__=='__main__':
    gui=Gui()